In this notebook, we will introduce one of the most powerful ideas in programming: the variable.
A variable is a container that we can reference by name that is associated with a particular value. The value is assigned to the variable using the the =
operator, which we might read as is set to the value of.
For example, consider the following assignment statement:
message="Hello World"
Here, we create a named container message
and put the value Hello World
into it.
When we refer to the variable as part of another expression, we can then access the value it contains and use that in our expression, as the following example demonstrates:
In [ ]:
# Create the message variable and assign the value "Hello World" to it
message="Hello World"
# Use the variable in a print statement
# The print statement retrieves the value assigned to the variable and displays the value
print(message)
Try changing the message in the previous code cell and re-running it. Does it behave as you expect?
You may remember from the Getting Started WIth Notebooks.ipynb
notebook that if the last statement in a code cell returns a value, the value will be displayed as the output of the code cell when the cell contents have been executed.
If you place the name of a variable, or one or more comma separated variables, on the last line of a code cell, the value will be displayed.
What do you think the output of the following cell will be? Run the cell to find out.
In [ ]:
message
You can assign whatever object you like to a variable.
For example, we can assign numbers to them and do sums with them:
In [1]:
#Assign raw numbers to variables
apples=5
oranges=10
#Do a sum with the values represented by the variables and assign the result to a new variable
items_in_basket = apples + oranges
#Display the resulting value as the cell output
items_in_basket
Out[1]:
See if you can add the count of a new set of purchases to the number of items in your basket in the cell above. For example, what if you also bought 3 pears. And a bunch of bananas.
In [1]:
%run 'Set-up.ipynb'
%run 'Loading scenes.ipynb'
%run 'vrep_models/PioneerP3DX.ipynb'
The original programme appears in the code cell below.
HINT: think variables...
In [6]:
%%vrepsim '../scenes/OU_Pioneer.ttt' PioneerP3DX
import time
#side 1
robot.move_forward()
time.sleep(1)
#turn 1
robot.rotate_left(1.8)
time.sleep(0.45)
#side 2
robot.move_forward()
time.sleep(1)
#turn 2
robot.rotate_left(1.8)
time.sleep(0.45)
#side 3
robot.move_forward()
time.sleep(1)
#turn 3
robot.rotate_left(1.8)
time.sleep(0.45)
#side 4
robot.move_forward()
time.sleep(1)
Using the above programme as a guide, see if you can write a programme in the code cell below that makes it easier to maintin and simplifies the act of changing the numerical parameter values.
In [ ]:
%%vrepsim '../scenes/OU_Pioneer.ttt' PioneerP3DX
import time
#YOUR CODE HERE
How did you get on?
How easy is is to change the side length now? Or find a new combination of the turn speed and turn angle to turn through ninety degrees (or thereabouts?). Try it and see...
Here's the programme I came up with: I used three variables, one for side length, one for turn time, and one for turn speed. Feel free to try running and modifying this programme too...
In [ ]:
%%vrepsim '../scenes/OU_Pioneer.ttt' PioneerP3DX
import time
side_length_time=1
turn_speed=1.8
turn_time=0.45
#side 1
robot.move_forward()
time.sleep(side_length_time)
#turn 1
robot.rotate_left(turn_speed)
time.sleep(turn_time)
#side 2
robot.move_forward()
time.sleep(side_length_time)
#turn 2
robot.rotate_left(turn_speed)
time.sleep(turn_time)
#side 3
robot.move_forward()
time.sleep(side_length_time)
#turn 3
robot.rotate_left(turn_speed)
time.sleep(turn_time)
#side 4
robot.move_forward()
time.sleep(side_length_time)
In the next two notebooks on this theme, we'll see how to cut out some of the repetition.